package main

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"os"
	"time"
)

func main(){
	// 定义一个map 用于存放点名次数
	stuMap := make(map[string]int)
	stuMap = map[string]int{
		"a" : 0,
		"b": 0,
		"c": 0,
		"d": 0,
		"e": 0,
	}
	//读取文件
	openFile, err := os.OpenFile("D:/random.txt", os.O_RDWR|os.O_CREATE, 777)

	// json --> map
	err = json.Unmarshal([]byte(openFile), &stuMap)
	if err != nil {
		fmt.Println("读取文件失败:",err)
	}

	lst4 := []string{"a", "b", "c", "d", "e"}

	fmt.Println("点名开始")
	for {
		var anw string
		fmt.Print("输入任意键开始；按q退出")
		fmt.Scanln(&anw)
		if anw == "q"{
			break
		}else{
			rand.Seed(time.Now().Unix())
			nu := rand.Intn(len(lst4))
			fmt.Printf("抽到了 %s \n", lst4[nu])
			name := lst4[nu]
			stuMap[name] = stuMap[name] + 1

			//map --> json
			jsoninfo, err := json.Marshal(stuMap)
			if err != nil {
				fmt.Println("map转换失败:",err)
			}
			// 写入文件
			err = os.WriteFile("./random.txt",jsoninfo,0644)
			if err != nil {
				fmt.Println("写入失败:",err)
			}
			lst4 = append(lst4[:nu], lst4[nu+1:]...)
			fmt.Println(lst4)
			if len(lst4) == 0{
				break
			}
		}
	}


}